import {Injectable} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd/message';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Config} from './config';
import {Router} from '@angular/router';
import axios from 'axios'

@Injectable()
export class Http {
  constructor(public msg: NzMessageService, public httpClient: HttpClient, private router: Router) {

  }

  public request(url: string, data: object, callbackFun) {
    axios.post(Config.API_URL + url, data, {
      headers: {
        'Content-Type': 'application/json',
        'token': localStorage.getItem('token')
      }
    })
      .then((result) => {
        console.log(result.data);
        if (result.data.status == -2) {
          this.msg.error('token过期，请重新登录');
          this.router.navigate(['login']);
        }
        return callbackFun(result.data);
      })
      .catch(function (error) {
        return callbackFun(error.data);
      });
  }
  public upload(url:string,param:object,callbackFun){
      axios
        .post(Config.API_URL + url,  param, {
          headers: {
            'Content-Type': 'application/json',
            'token': localStorage.getItem('token')
          },
        })
        .then((result)=> {
          if (result.data.status == -2) {
            this.msg.error('token过期，请重新登录');
            this.router.navigate(['login']);
          }
          return callbackFun(result.data);
        })
        .catch((error)=> {
          return callbackFun(error.data);
        })
  }
}

interface ValueObject {
  status?: number
}
